package com.timothy.junk.gfund.activity;

import java.util.Date;

import com.timothy.junk.R;
import com.timothy.gfund.bean.NotifySettingBean;
import com.timothy.gfund.service.NotifyService;
import com.timothy.junk.ActivityManage;
import com.timothy.junk.common.util.DateUtil;
import com.timothy.junk.common.util.SPUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class GFundSetting extends Activity {
	public static final String LOG_TAG = "GFundSetting";
	
	public static final String SERVIC_CLASS = "com.timothy.gfund.service.NotifyService";
	
	Context mContext;
	SharedPreferences sp;
	
	TimePicker startTime;
	TimePicker endTime;
	
	int timeInterval = 30;
	int startHour = 9;
	int startMinute = 30;
	int endHour = 15;
	int endMinute = 5;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gfund_sys_setting);
        ActivityManage.add(this);
        mContext = getApplicationContext();
        sp = this.getSharedPreferences("GFundApp",MODE_PRIVATE);
        
        startTime = (TimePicker) findViewById(R.id.startTime);
        startTime.setIs24HourView(true);
        endTime = (TimePicker) findViewById(R.id.endTime);
        endTime.setIs24HourView(true);
        
        NotifySettingBean saveBean = SPUtil.getNotifySettingBean(sp);
        displayAllSetting(saveBean);

        startTime.setOnTimeChangedListener(new OnTimeChangedListener() {
			@Override
			public void onTimeChanged(TimePicker view, int hour, int minute) {
				startHour = hour;
				startMinute = minute;
			}
		});
        endTime.setOnTimeChangedListener(new OnTimeChangedListener() {
			@Override
			public void onTimeChanged(TimePicker view, int hour, int minute) {
				 endHour = hour;
				 endMinute = minute;
			}
		});
        
//        saveBtn = (Button) findViewById(R.id.saveBtn);
//        saveBtn.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				
//		    	String ti =  tiEditText.getText().toString();
//		    	int timeInterval = Integer.valueOf(ti.replace("S", ""));
//		    	NotifySettingBean sBean = new NotifySettingBean(notifyFlag,timeInterval,startHour,startMinute,endHour,endMinute);
//		    	if(getNotify(sBean) && !ServiceUtil.isServiceRunning(mContext, SERVIC_CLASS)){
//		    		startNotifyService();
//		    	}
//		    	
//		    	SPUtil.setNotifySettingBean(sBean, sp);
//			}
//        });
    }

    public void displayAllSetting(NotifySettingBean bean){
        if(bean.getStartHour()>0){
        	startTime.setCurrentHour(startHour);
        }else{
        	startTime.setCurrentHour(9);
        }
        if(bean.getStartMinute()>0){
        	startTime.setCurrentMinute(startMinute);
        }else{
        	startTime.setCurrentMinute(30);
        }
        if(bean.getEndHour()>0){
        	endTime.setCurrentHour(endHour);
        }else{
        	endTime.setCurrentHour(15);
        }
        if(bean.getEndMinute()>0){
        	endTime.setCurrentMinute(endMinute);
        }else{
        	endTime.setCurrentMinute(5);
        }
    }
    
	 public boolean getNotify(NotifySettingBean nBean){
		  String notfiyFlag = nBean.getIsNotify();
		  Date startTime = DateUtil.getTime(nBean.getStartHour(), nBean.getStartMinute());
		  Date endTime = DateUtil.getTime(nBean.getEndHour(), nBean.getEndMinute());
		  Date nowTime = DateUtil.getNowTime();
		  Log.i(LOG_TAG,"startTime:" + startTime.getTime());
		  Log.i(LOG_TAG,"endTime:" + endTime.getTime());
		  Log.i(LOG_TAG,"nowTime:" + nowTime.getTime());
		  if("Yes".equalsIgnoreCase(notfiyFlag)){
			  if(nowTime.getTime()>=startTime.getTime() && endTime.getTime() <= endTime.getTime()){
				  return true;
			  }
		  }
		  return false;
	}
    
	//stop notify service
//  Intent intent = new Intent(mContext, NotifyService.class);
//  stopService(intent);
    //  Toast.makeText(mContext, "Notify Service Stopped!", Toast.LENGTH_LONG).show();
    
	public void startNotifyService() {
		Intent intent = new Intent(mContext, NotifyService.class);
		startService(intent);
		Toast.makeText(mContext, "Notify Service Started!", Toast.LENGTH_LONG).show();
	}

}
